package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppVisitor;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 访客记录
 */
@Repository
public class AppVisitorDao extends BaseDao<AppVisitor> {

    /**
     * 查询列表
     */
    public List<AppVisitor> list(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_visitor where 1=1  ");
        if(StringEx.isNotNull(paraMap.get("startDate"))){
            sbStr.append(" and DATE_FORMAT(gusttime, '%Y-%m-%d')  >=  ? ");
            paramList.add(paraMap.get("startDate"));
        }
        if(StringEx.isNotNull(paraMap.get("endDate"))){
            sbStr.append(" and DATE_FORMAT(gusttime, '%Y-%m-%d')  <=  ? ");
            paramList.add(paraMap.get("endDate"));
        }
        if(StringEx.isNotNull(paraMap.get("nick"))){
            sbStr.append(" and nick like ? ");
            paramList.add("%"+paraMap.get("nick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("phone"))){
            sbStr.append(" and phone = ? ");
            paramList.add(paraMap.get("phone"));
        }
        if(StringEx.isNotNull(paraMap.get("gustname"))){
            sbStr.append(" and gustname like ? ");
            paramList.add("%"+paraMap.get("gustname")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("gustphone"))){
            sbStr.append(" and gustphone = ? ");
            paramList.add(paraMap.get("gustphone"));
        }
        if(StringEx.isNotNull(paraMap.get("gustsex"))){
            sbStr.append(" and gustsex = ? ");
            paramList.add(paraMap.get("gustsex"));
        }
        if(paraMap.containsKey("visitorType") && paraMap.get("visitorType").toString().equals("1")) {
            sbStr.append(" and userid = ? ");
            paramList.add(paraMap.get("userId"));
        }
        if(paraMap.containsKey("visitorType") && paraMap.get("visitorType").toString().equals("2")) {
            sbStr.append(" and gustid = ? ");
            paramList.add(paraMap.get("userId"));
        }
        sbStr.append(" order by gusttime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppVisitor.class,splitPage);
    }


    /**
     * 详情
     */
    public AppVisitor get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_visitor where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppVisitor.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppVisitor haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_visitor where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete from app_visitor where userid = ? or gustid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(userId);
        paramList.add(userId);
        this.del(sql, paramList);
    }



    /**
     * 更新用户头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_visitor SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新用户昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_visitor SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
     * 更新来访人头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateGustImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_visitor SET gustpic = ?  WHERE gustid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新来访人昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateGustNick(Integer userid,String nick){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_visitor SET gustname = ?  WHERE  gustid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
    * 更新来访用户性别
    * @Param: [userid, sex]
    * @return: void
    * @Author: zhouyunyi
    * @Date: 2021/07/28
    * @Time: 18:23
    */
    public void updateLfSex(Integer userid,Integer sex){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(sex)){
            return;
        }
        String sql = " UPDATE app_visitor SET gustsex = ?  WHERE gustid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(sex);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    public int visitorCount(Integer userId) {
        StringBuilder sbStr = new StringBuilder("select * from app_visitor where userid = ? ");
        ArrayList<Object> paramList = new ArrayList<>();
        paramList.add(userId);
        List<AppVisitor> appVisitors = this.queryList(sbStr.toString(), paramList, AppVisitor.class);

        return appVisitors.size();
    }

    public AppVisitor findByVisitorLog(Integer from, Integer to) {
        StringBuilder sbStr = new StringBuilder("select * from app_visitor where gustid = ? and userid = ? limit 1");
        ArrayList<Object> paramList = new ArrayList<>();
        paramList.add(from);
        paramList.add(to);

        return this.queryObj(sbStr.toString(), paramList, AppVisitor.class);
    }
}
